package com.nantian.logic.controller.campusRelated;

import com.cuit.springboot.gdao.place.CityMapper;
import com.cuit.springboot.gdao.place.ProvinceMapper;
import com.cuit.springboot.gentry.place.City;
import com.cuit.springboot.gentry.place.Province;
import com.nantian.logic.utils.UniversalResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/province")
public class ProvinceController {
    @Autowired
    ProvinceMapper provinceMapper;
    @Autowired
    CityMapper cityMapper;

    @PostMapping(value = "/update")
    public UniversalResult update(@RequestBody Map map) {
//        activityRepository.update(activityEntity.getActId(),activityEntity.getActName());
//        activityMapper.updateConfirm(map);
        return new UniversalResult<>(UniversalResult.SUCCESS, "更新成功");
    }

    @PostMapping(value = "/selectAll")
    public UniversalResult select() {

        return new UniversalResult<>(UniversalResult.SUCCESS, "查询成功", provinceMapper.selectAll());
    }

    @PostMapping(value = "/selectWithCity")
    public UniversalResult selectWithCity() {
        List provinceCities = new ArrayList<>();
        List<Province> provinceList = provinceMapper.selectAll();
        List<City> cityList = cityMapper.selectAll();
        provinceList.forEach(province -> {
            List<City> cities = cityList.stream().filter(city -> Objects.equals(province.getId(), city.getProid())).collect(Collectors.toList());
            Map<String, Object> parent = new HashMap<>();
            List<Map> childrenDetail = new ArrayList<>();
            parent.put("label", province.getProvince());
            parent.put("value", province.getId());
            cities.forEach(city -> {
                Map<String, Object> childrenKeyVal = new HashMap<>();
                childrenKeyVal.put("label", city.getCity());
                childrenKeyVal.put("value",city.getId());
                childrenKeyVal.put("isLeaf",true);
                childrenDetail.add(childrenKeyVal);
            });
            parent.put("children",childrenDetail);
            provinceCities.add(parent);
        });
        return new UniversalResult<>(UniversalResult.SUCCESS, "",provinceCities);
    }

}